package cn.blue.org.service.impl;

import cn.blue.base.config.BussinessException;
import cn.blue.base.query.BaseQuery;
import cn.blue.base.util.BaiduAuditUtils;
import cn.blue.base.util.PageList;
import cn.blue.org.domain.Employee;
import cn.blue.org.domain.Shop;
import cn.blue.org.domain.ShopAuditLog;
import cn.blue.org.mapper.EmployeeMapper;
import cn.blue.org.mapper.ShopAuditLogMapper;
import cn.blue.org.mapper.ShopMapper;
import cn.blue.org.service.IShopService;
import cn.blue.base.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hui
 * @since 2022-10-15
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    // 注入邮件对象
    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public void settlement(Shop shop) {
        verify(shop);
        //判断店铺是否被注册 根据店铺名称来判断
        Shop loadShop = shopMapper.findByName(shop.getName());
        //从数据库查出数据判断不为空 店铺则存在
        if (loadShop !=null){
            throw new BussinessException("店铺已经存在，请重新输入");
        }
        //判断管理员是否已经存在 并且以绑定了店铺
        //获取前端所有数据 用Employee对象接收
        Employee admin = shop.getAdmin();
        //通过admin中姓名在员工表查询数据
        Employee loadEmp = employeeMapper.findByName(admin.getUsername());
        //判断管理员是否为空并且已经绑定了店铺
        if(loadEmp !=null && loadEmp.getShopId() !=null){
            throw new BussinessException("管理员已经绑定了店铺，请重新输入！！");
        }
        //判断管理员没有被注册就进行管理员添加
        if(loadEmp == null){
            loadEmp = admin;
            employeeMapper.add(loadEmp);
        }
        shopMapper.add(shop);
        //设置管理员信息到店铺中进行保存
        System.out.println(admin.getId());
        shop.setAdminId(admin.getId());
        //修改数据库店铺信息
        shopMapper.update(shop);
        //把店铺id给到管理员shop_id
        admin.setShopId(shop.getId());
        //修改数据库管理员信息
        employeeMapper.update(admin);
    }



    private void verify(Shop shop) {
        if(StringUtils.isEmpty(shop.getName())){
            //名称为空 抛出异常
            throw new BussinessException("店铺名称不能为空");
        }
        Boolean boo = BaiduAuditUtils.TextCensor(shop.getName());
        if (!boo){
            throw new BussinessException("请输入合法信息");
        }
        if(!BaiduAuditUtils.ImgCensor(shop.getLogo())){
            throw new BussinessException("店铺logo不合理");
        }

        if(StringUtils.isEmpty(shop.getAddress())){
            //店铺地址为空 抛出异常
            throw new BussinessException("店铺地址不能为空");
        }
        if(StringUtils.isEmpty(shop.getTel())){
            //店铺电话为空 抛出异常
            throw new BussinessException("店铺电话不能为空");
        }
        if(StringUtils.isEmpty(shop.getAdmin().getUsername())){
            //店铺管理人姓名为空 抛出异常
            throw new BussinessException("管理人姓名不能为空");
        }
        if(StringUtils.isEmpty(shop.getAdmin().getPassword())){
            //店铺管理人密码为空 抛出异常
            throw new BussinessException("管理人密码不能为空");
        }
        if(StringUtils.isEmpty(shop.getAdmin().getPhone())){
            //店铺管理人电话为空 抛出异常
            throw new BussinessException("管理人电话不能为空");
        }
        if(StringUtils.isEmpty(shop.getAdmin().getEmail())){
            //店铺管理人邮箱为空 抛出异常
            throw new BussinessException("管理人邮箱不能为空");
        }
    }

    //店铺审核通过接口
    @Override
    public void auditPass(ShopAuditLog shopAuditLog) {

        Shop shop = new Shop();
        //给shop对象设置前端获取的店铺id
        shop.setId(shopAuditLog.getShopId());
        //把状态改为审核通过
        shop.setState(1);
        //执行修改店铺信息数据库修改
        shopMapper.update(shop);
        //写死审核人id
        shopAuditLog.setAuditId(123L);
        //把获取信息添加到日志表中
        shopAuditLogMapper.add(shopAuditLog);
        //发送邮件
        // 1.创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        // 2.发送复杂邮件的工具类
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("1576546578@qq.com");
            helper.setSubject("宠物之家审核结果通知");
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>恭喜审核通过，请点击下方链接激活店铺</h1>"+
                    "http://localhost:8080/shop/active/"+shopAuditLog.getShopId(),true);
            // 4.添加附件
            helper.addAttachment("好东西.jpg",new File("D:\\study\\resource\\学习资料\\1.jpg"));
            // 5.设置收件人
            helper.setTo("1576546578@qq.com");
        } catch (MessagingException e) {
            e.printStackTrace();
            throw new BussinessException("发送邮件错误");
        }

        // 6.发送邮件
        javaMailSender.send(mimeMessage);
    }

    @Override
    public void active(Long id) {
        Shop shop = new Shop();
        //设置店铺id
        shop.setId(id);
        //状态改为已激活
        shop.setState(2);
        shopMapper.update(shop);

    }

    @Override
    public void auditReject(ShopAuditLog shopAuditLog) {
        Shop shop = new Shop();
        //给shop对象设置前端获取的店铺id
        shop.setId(shopAuditLog.getShopId());
        //把状态改为审核驳回
        shop.setState(-1);
        //执行修改店铺信息数据库修改
        shopMapper.update(shop);
        //写死审核人id
        shopAuditLog.setAuditId(123L);
        //把获取信息添加到日志表中
        shopAuditLogMapper.add(shopAuditLog);
        //发送邮件
        // 1.创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        // 2.发送复杂邮件的工具类
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("1576546578@qq.com");
            helper.setSubject("宠物之家审核结果通知");
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>非常抱歉审核失败！请点击下方链接重新注册</h1>"+
                    "http://localhost:8081/#/registerAgain?shopId="+shopAuditLog.getShopId(),true);
            // 4.添加附件
            helper.addAttachment("美女哦.jpg",new File("D:\\study\\resource\\学习资料\\1.jpg"));
            // 5.设置收件人
            helper.setTo("1576546578@qq.com");
        } catch (MessagingException e) {
            e.printStackTrace();
            throw new BussinessException("发送邮件错误");
        }
        // 6.发送邮件
        javaMailSender.send(mimeMessage);

    }

    @Override
    public PageList queryByPage(BaseQuery query) {
        PageList pageList = super.queryByPage(query);
        List<Shop> rows = pageList.getRows();
        List<ShopAuditLog> logs = shopAuditLogMapper.findAll();
        for (Shop shop : rows) {
            for (ShopAuditLog log : logs) {
                if (shop.getId()==log.getShopId()) {
                    List<ShopAuditLog> auditLogs = shop.getAuditLogs();
                    auditLogs.add(log);
                }
            }
        }
        return pageList;
    }
}
